package com.handmark.pulltorefresh.library;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.handmark.pulltorefresh.library.internal.LoadingLayout;
import com.ss.android.article.base.feature.feed.activity.ArticleRecentFragment;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.newmedia.webview.SSWebView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public abstract class PullToRefreshBase<T extends View> extends LinearLayout {
    private static IAnimationStyle f;
    public T mRefreshableView;
    public boolean mOverScrollEnabled = true;
    protected List mLoadingLayoutList = new ArrayList();
    boolean mLayoutVisibilityChangesEnabled = true;
    Interpolator mScrollAnimationInterpolator;
    private SmoothScrollRunnable mCurrentSmoothScrollRunnable;
    private PullToRefreshBase$g B;
    private boolean C;
    private float D;
    private float E;
    private float F;
    private boolean G;
    private int mTouchSlop;
    private float mLastMotionX;
    private float mLastMotionY;
    private float mInitialMotionX;
    private float mInitialMotionY;
    private boolean mIsBeingDragged = false;
    private State mState = State.RESET;
    private Mode mMode = Mode.getDefault();
    private Mode mCurrentMode;
    private boolean mShowViewWhileRefreshing = true;
    private boolean mScrollingWhileRefreshingEnabled = false;
    private boolean mFilterTouchEvents = true;
    private boolean s;
    private boolean t;
    private IAnimationStyle mLoadingAnimationStyle = AnimationStyle.getDefault();
    private LoadingLayout mHeaderLayout;
    private LoadingLayout mFooterLayout;
    private OnRefreshListener mOnRefreshListener;
    private OnRefreshListener2 mOnRefreshListener2;
    private WeakReference<OnTouchHook> mOnTouchHookRef;

    public PullToRefreshBase(Context context) {
        super(context);
        this.init(context, null);
    }

    public PullToRefreshBase(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(context, attrs);
    }

    public PullToRefreshBase(Context context, Mode mode) {
        super(context);
        this.mMode = mode;
        this.init(context, null);
    }

    public PullToRefreshBase(Context context,
                             Mode mode,
                             IAnimationStyle animationStyle) {
        super(context);
        this.mMode = mode;
        this.mLoadingAnimationStyle = animationStyle;
        this.init(context, null);
    }

    public static void setAnimationStyle(IAnimationStyle animationStyle) {
        PullToRefreshBase.f = animationStyle;
    }

    private void smoothScrollTo(int newScrollValue, long duration, OnSmoothScrollListener listener) {
        if (this.mCurrentSmoothScrollRunnable != null) {
            mCurrentSmoothScrollRunnable.mContinueRunning = false;
            PullToRefreshBase.this.removeCallbacks(mCurrentSmoothScrollRunnable);
        }

        int oldScrollValue;
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                oldScrollValue = this.getScrollX();
                break;
            }
            default: {
                oldScrollValue = this.getScrollY();
                break;
            }
        }

        if (oldScrollValue != newScrollValue) {
            if (this.mScrollAnimationInterpolator == null) {
                this.mScrollAnimationInterpolator = new DecelerateInterpolator();
            }

            this.mCurrentSmoothScrollRunnable = new SmoothScrollRunnable(oldScrollValue,
                    newScrollValue,
                    duration,
                    listener);
            long delayMillis = 0;
            if (delayMillis > 0) {
                this.postDelayed(this.mCurrentSmoothScrollRunnable, delayMillis);
            } else {
                this.post(this.mCurrentSmoothScrollRunnable);
            }
        }
    }

    private void smoothScrollTo(int scrollValue, OnSmoothScrollListener listener) {
        this.smoothScrollTo(scrollValue, (long) this.getPullToRefreshScrollDuration(), listener);
    }

    private void setState(boolean arg3, State state, boolean[] params) {
        this.mState = state;
        switch (mState) {
            case RESET:
                this.onReset();
                return;
            case PULL_TO_REFRESH:
                switch (mCurrentMode) {
                    case PULL_FROM_END:
                        this.mFooterLayout.f();
                        return;
                    case PULL_FROM_START:
                        this.mHeaderLayout.f();
                        return;
                    default:
                        return;
                }
            case RELEASE_TO_REFRESH:
                switch (mCurrentMode) {
                    case PULL_FROM_END: {
                        this.mFooterLayout.h();
                        return;
                    }
                    case PULL_FROM_START: {
                        this.mHeaderLayout.h();
                        return;
                    }
                    default:
                        return;
                }
            case REFRESHING:
            case MANUAL_REFRESHING:
                this.onRefreshing(arg3, params[0]);
                break;
        }
    }

    public abstract T createRefreshableView(Context arg1, AttributeSet arg2);

    protected final LoadingLayout createLoadingLayout(Context arg3, Mode arg4, TypedArray arg5) {
        LoadingLayout v0 = this.mLoadingAnimationStyle.createLoadingLayout(arg3, arg4, this.getPullToRefreshScrollDirection(), arg5);
        v0.setVisibility(INVISIBLE);
        return v0;
    }

    protected void handleStyledAttributes(TypedArray arg1) {
    }

    public abstract boolean isReadyForPullStart();

    protected LoadingLayoutProxy createLoadingLayoutProxy(boolean includeStart, boolean includeEnd) {
        LoadingLayoutProxy loadingLayoutProxy = new LoadingLayoutProxy();
        if (includeStart && this.mMode.showHeaderLoadingLayout()) {
            loadingLayoutProxy.addLayout(this.mHeaderLayout);
        }
        if (includeEnd && this.mMode.showFooterLoadingLayout()) {
            loadingLayoutProxy.addLayout(this.mFooterLayout);
        }
        return loadingLayoutProxy;
    }

    protected final void smoothScrollTo(int scrollValue) {
        this.smoothScrollTo(scrollValue, ((long) this.getPullToRefreshScrollDuration()), null);
    }

    public void onPtrRestoreInstanceState(Bundle bundle) {
    }

    protected final void addViewInternal(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
    }

    public final void a(State arg2, boolean[] arg3) {
        this.setState(true, arg2, arg3);
    }

    public void addView(View arg3, int arg4, ViewGroup.LayoutParams arg5) {
        View v0 = this.getRefreshableView();
        if ((v0 instanceof ViewGroup)) {
            ((ViewGroup) v0).addView(arg3, arg4, arg5);
            return;
        }

        throw new UnsupportedOperationException("Refreshable View is not a ViewGroup so can\'t addView");
    }

    private void init(Context context, AttributeSet attributeSet) {
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL:
                this.setOrientation(HORIZONTAL);
                break;
            default:
                this.setOrientation(VERTICAL);
                break;
        }

        this.setGravity(Gravity.CENTER);
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, com.ss.android.common.dd.PullToRefresh);
        if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrMode)) {
            this.mMode = Mode.mapIntToValue(typedArray.getInteger(com.ss.android.common.dd.PullToRefresh_ptrMode, 0));
        }

        if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrAnimationStyle)) {
            this.mLoadingAnimationStyle = AnimationStyle.mapIntToValue(
                    typedArray.getInteger(com.ss.android.common.dd.PullToRefresh_ptrAnimationStyle, 0));
        }

        if (PullToRefreshBase.f != null) {
            this.mLoadingAnimationStyle = PullToRefreshBase.f;
        }

        this.mRefreshableView = this.createRefreshableView(context, attributeSet);
        super.addView(this.mRefreshableView, -1, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        this.mHeaderLayout = this.createLoadingLayout(context, Mode.PULL_FROM_START, typedArray);
        this.mLoadingLayoutList.add(this.mHeaderLayout);
        this.mFooterLayout = this.createLoadingLayout(context, Mode.PULL_FROM_END, typedArray);
        if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrRefreshableViewBackground)) {
            Drawable background = typedArray.getDrawable(com.ss.android.common.dd.PullToRefresh_ptrRefreshableViewBackground);
            if (background != null) {
                this.mRefreshableView.setBackgroundDrawable(background);
            }
        } else if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrAdapterViewBackground)) {
            android.support.design.a.warnDeprecation("ptrAdapterViewBackground", "ptrRefreshableViewBackground");
            Drawable background = typedArray.getDrawable(com.ss.android.common.dd.PullToRefresh_ptrAdapterViewBackground);
            if (background != null) {
                this.mRefreshableView.setBackgroundDrawable(background);
            }
        }
        if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrOverScroll)) {
            this.mOverScrollEnabled = typedArray.getBoolean(com.ss.android.common.dd.PullToRefresh_ptrOverScroll, true);
        }
        if (typedArray.hasValue(com.ss.android.common.dd.PullToRefresh_ptrScrollingWhileRefreshingEnabled)) {
            this.mScrollingWhileRefreshingEnabled = typedArray.getBoolean(com.ss.android.common.dd.PullToRefresh_ptrScrollingWhileRefreshingEnabled, false);
        }
        this.handleStyledAttributes(typedArray);
        typedArray.recycle();
        this.updateUIForMode();
    }

    protected void onRefreshing(boolean shouldCallRefreshListener, boolean doScroll) {
        if (this.mMode.showHeaderLoadingLayout()) {
            this.mHeaderLayout.refreshing();
        }

        if (this.mMode.showFooterLoadingLayout()) {
            this.mFooterLayout.refreshing();
        }

        if (doScroll) {
            if (mShowViewWhileRefreshing) {
                OnSmoothScrollListener onSmoothScrollListener = new OnSmoothScrollListener(shouldCallRefreshListener);
                switch (mCurrentMode) {
                    case PULL_FROM_END:
                    case MANUAL_REFRESH_ONLY:
                        this.smoothScrollTo(this.getFooterSize(), onSmoothScrollListener);
                        return;
                    default:
                        this.smoothScrollTo(-this.getHeaderSize(), onSmoothScrollListener);
                        return;
                }
            } else {
                this.smoothScrollTo(0);
                return;
            }
        } else {
            if (shouldCallRefreshListener) {
                this.callRefreshListener();
            }
        }
    }

    public abstract boolean isReadyForPullEnd();

    public final void b(int arg7) {
        float v0;
        float v1;
        float v5 = 2f;
        if ((this.mMode.permitsPullToRefresh()) && ((this.mScrollingWhileRefreshingEnabled) || !this.c())) {
            if (this.mCurrentMode == Mode.PULL_FROM_END || !this.h()) {
                arg7 = ((int) ((((float) arg7)) * v5));
            } else {
                int v2 = this.mHeaderLayout.getPtrHeaderExtraSize();
                if (this.s) {
                    if (arg7 > 0) {
                    } else {
                        arg7 = ((int) ((((float) arg7)) * v5));
                    }
                } else {
                    switch (getPullToRefreshScrollDirection()) {
                        case HORIZONTAL: {
                            v1 = this.mInitialMotionX;
                            v0 = this.mLastMotionX;
                            break;
                        }
                        default: {
                            v1 = this.mInitialMotionY;
                            v0 = this.mLastMotionY;
                            break;
                        }
                    }

                    float v3 = Math.abs((((float) arg7)) + v1 - v0);
                    if (v3 <= (((float) v2))) {
//                        goto label_22;
                    } else {
                        v1 = (((float) (v2 + (((int) ((v3 - (((float) v2))) * v5)))))) - Math.abs(v1 - v0);
                        int v0_1 = arg7 > 0 ? 1 : -1;
                        arg7 = ((int) ((((float) v0_1)) * v1));
                    }
                }
            }

            label_22:
            switch (getPullToRefreshScrollDirection()) {
                case HORIZONTAL: {
                    this.mInitialMotionX += ((float) arg7);
                    break;
                }
                default: {
                    this.mInitialMotionY += ((float) arg7);
                    break;
                }
            }

            this.m();
        }
    }

    public void onPtrSaveInstanceState(Bundle arg1) {
    }

    public final boolean c() {
        return this.mState == State.REFRESHING || this.mState == State.MANUAL_REFRESHING;
    }

    public void onRefreshComplete() {
        if (this.c()) {
            if (this.h()) {
                this.t = true;
            }
            this.setState(true, State.RESET, new boolean[0]);
        }
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        float y = motionEvent.getY();
        switch (motionEvent.getAction()) {
            case 0:
                this.F = y;
                this.G = false;
                break;
            case 1:
                if (this.G && this.E - y <= (float) this.mTouchSlop) {
                    motionEvent.setAction(MotionEvent.ACTION_CANCEL);
                }
                this.G = false;
                break;
        }

        if ((this.mIsBeingDragged) && this.getScrollY() == 0 && y < this.F) {
            motionEvent.setAction(0);
            this.mIsBeingDragged = false;
            this.E = motionEvent.getY();
            this.G = true;
        }

        return super.dispatchTouchEvent(motionEvent);
    }

    public final void e() {
        if (!this.c()) {
            this.setState(false, State.MANUAL_REFRESHING, new boolean[]{true});
        }
    }

    protected void onReset() {
        this.mIsBeingDragged = false;
        this.mLayoutVisibilityChangesEnabled = true;
        this.mHeaderLayout.reset();
        this.mFooterLayout.reset();
        if ((this.t) || !this.h()) {
            this.smoothScrollTo(0);
            this.s = false;
        } else {
            int v0 = this.mHeaderLayout.getPtrHeaderExtraSize();
            int v1 = v0 / 3;
            int v2 = Math.abs(this.getScrollY());
            if (!this.s && v2 < v1 || (this.s) && v2 < v0) {
                this.s = false;
                this.smoothScrollTo(0);
            } else {
                this.s = true;
                this.smoothScrollTo(-v0);
            }
        }
        this.t = false;
    }

    final void callRefreshListener() {
        if (this.mOnRefreshListener != null) {
            ((SSWebView) this.getRefreshableView()).reload();
        } else if (this.mOnRefreshListener2 != null && this.mCurrentMode == Mode.PULL_FROM_START) {
            this.mOnRefreshListener2.onPullDownToRefresh(this);
        }
    }

    public final Mode getCurrentMode() {
        return this.mCurrentMode;
    }

    public final boolean getFilterTouchEvents() {
        return this.mFilterTouchEvents;
    }

    public final void setFilterTouchEvents(boolean arg1) {
        this.mFilterTouchEvents = arg1;
    }

    protected final LoadingLayout getFooterLayout() {
        return this.mFooterLayout;
    }

    protected final int getFooterSize() {
        return this.mFooterLayout.getContentSize();
    }

    public final LoadingLayout getHeaderLayout() {
        return this.mHeaderLayout;
    }

    public List getHeaderLayoutList() {
        return this.mLoadingLayoutList;
    }

    protected final int getHeaderSize() {
        return this.mHeaderLayout.getContentSize();
    }

    private LinearLayout.LayoutParams getLoadingLayoutLayoutParams() {
        LinearLayout.LayoutParams v0;
        int v3 = -1;
        int v2 = -2;
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                v0 = new LinearLayout.LayoutParams(v2, v3);
                break;
            }
            default: {
                v0 = new LinearLayout.LayoutParams(v3, v2);
                break;
            }
        }

        return v0;
    }

    public final ILoadingLayout getLoadingLayoutProxy() {
        return this.createLoadingLayoutProxy(true, true);
    }

    private int getMaximumPullScroll() {
        int v0;
        float v2 = 2f;
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                v0 = Math.round((((float) this.getWidth())) / v2);
                break;
            }
            default: {
                v0 = Math.round((((float) this.getHeight())) / v2);
                break;
            }
        }

        return v0;
    }

    public final Mode getMode() {
        return this.mMode;
    }

    public final void setMode(Mode arg2) {
        if (arg2 != this.mMode) {
            this.mMode = arg2;
            this.updateUIForMode();
        }
    }

    public abstract Orientation getPullToRefreshScrollDirection();

    protected int getPullToRefreshScrollDuration() {
        return 200;
    }

    protected int getPullToRefreshScrollDurationLonger() {
        return 325;
    }

    public final T getRefreshableView() {
        return this.mRefreshableView;
    }

    public final boolean getShowViewWhileRefreshing() {
        return this.mShowViewWhileRefreshing;
    }

    public final void setShowViewWhileRefreshing(boolean arg1) {
        this.mShowViewWhileRefreshing = arg1;
    }

    public final State getState() {
        return this.mState;
    }

    private boolean h() {
        boolean v0 = false;
        if (this.mHeaderLayout != null && (this.mHeaderLayout.i)) {
            v0 = true;
        }

        return v0;
    }

    private void i() {
        int v0;
        int v1 = 0;
        int v6 = ((int) ((((float) this.getMaximumPullScroll())) * 1.2f));
        int v5 = this.getPaddingLeft();
        int v4 = this.getPaddingTop();
        int v3 = this.getPaddingRight();
        int v2 = this.getPaddingBottom();
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                if (this.mMode.showHeaderLoadingLayout()) {
                    this.mHeaderLayout.setWidth(v6);
                    v0 = -v6;
                } else {
                    v0 = 0;
                }

                if (this.mMode.showFooterLoadingLayout()) {
                    this.mFooterLayout.setWidth(v6);
                    v3 = v0;
                    v0 = -v6;
                    v1 = v2;
                    v2 = v4;
                } else {
                    v3 = v0;
                    v0 = 0;
                    v1 = v2;
                    v2 = v4;
                }
                break;
            }
            case VERTICAL: {
                if (this.mMode.showHeaderLoadingLayout()) {
                    this.mHeaderLayout.setHeight(v6);
                    v0 = -v6;
                } else {
                    v0 = 0;
                }

                if (this.mMode.showFooterLoadingLayout()) {
                    this.mFooterLayout.setHeight(v6);
                    v1 = -v6;
                    v2 = v0;
                    v0 = v3;
                    v3 = v5;
                } else {
                    v2 = v0;
                    v0 = v3;
                    v3 = v5;
                }
                break;
            }
            default: {
                v1 = v2;
                v0 = v3;
                v2 = v4;
                v3 = v5;
                break;
            }
        }

        label_19:
        this.setPadding(v3, v2, v0, v1);
    }

    private void updateUIForMode() {
        LinearLayout.LayoutParams v0 = this.getLoadingLayoutLayoutParams();
        if (this == this.mHeaderLayout.getParent()) {
            this.removeView(this.mHeaderLayout);
        }

        if (this.mMode.showHeaderLoadingLayout()) {
            super.addView(this.mHeaderLayout, 0, ((ViewGroup.LayoutParams) v0));
        }

        if (this == this.mFooterLayout.getParent()) {
            this.removeView(this.mFooterLayout);
        }

        if (this.mMode.showFooterLoadingLayout()) {
            super.addView(this.mFooterLayout, -1, ((ViewGroup.LayoutParams) v0));
        }

        this.i();
        this.mCurrentMode = this.mMode != Mode.BOTH ? this.mMode : Mode.PULL_FROM_START;
    }

    private boolean k() {
        switch (this.mMode) {
            case PULL_FROM_END: {
                return this.isReadyForPullEnd();
            }
            case PULL_FROM_START: {
                return this.l();
            }
            case BOTH: {
                return this.isReadyForPullEnd() || this.l();
            }
        }

        return false;
    }

    private boolean l() {
        if (!this.isReadyForPullStart()) {
            if (this.h() && this.s && this.getScrollY() < 0) {
                return true;
            }
            return false;
        } else {
            return true;
        }
    }

    private void m() {
        int v0_1;
        int v1_1;
        float v0;
        float v1;
        float v5 = 2f;
        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                v1 = this.mInitialMotionX;
                v0 = this.mLastMotionX;
                break;
            }
            default: {
                v1 = this.mInitialMotionY;
                v0 = this.mLastMotionY;
                break;
            }
        }

        switch (mCurrentMode) {
            case PULL_FROM_END: {
                v1_1 = Math.round(Math.max(v1 - v0, 0f) / v5);
                v0_1 = this.getFooterSize();
                break;
            }
            default: {
                v0 = v1 - v0;
                if (this.h()) {
                    v1_1 = this.mHeaderLayout.getPtrHeaderExtraSize();
                    if (this.s) {
                        v0_1 = v0 <= 0f ? Math.round(Math.min(v0, 0f) / v5) : Math.round(Math.min(v0, ((float) v1_1)));
                        v0_1 -= v1_1;
                    } else {
                        if (v0 <= (((float) (-v1_1)))) {
                            v0_1 = Math.round((v0 + (((float) v1_1))) / v5 + (((float) (-v1_1))));
                        } else {
                            v0_1 = Math.round(Math.min(v0, 0f));
                        }
                    }
                } else {
                    v0_1 = Math.round(Math.min(v0, 0f) / v5);
                }

                label_28:
                v1_1 = v0_1;
                v0_1 = this.getHeaderSize();
                break;
            }
        }

        this.setHeaderScroll(v1_1);
        if (v1_1 != 0 && !this.c()) {
            float v2 = (((float) Math.abs(v1_1))) / (((float) v0_1));
            switch (mCurrentMode) {
                case PULL_FROM_END: {
                    this.mFooterLayout.b(v2);
                    break;
                }
                default: {
                    this.mHeaderLayout.b(v2);
                    break;
                }
            }

            if (this.mState != State.PULL_TO_REFRESH && v0_1 >= Math.abs(v1_1)) {
                this.setState(true, State.PULL_TO_REFRESH, new boolean[0]);
                return;
            }

            if (this.mState != State.PULL_TO_REFRESH) {
                return;
            }

            if (v0_1 >= Math.abs(v1_1)) {
                return;
            }

            this.setState(true, State.RELEASE_TO_REFRESH, new boolean[0]);
        }
    }

    public final boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        if (!this.mMode.permitsPullToRefresh()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action != MotionEvent.ACTION_CANCEL
                && action != MotionEvent.ACTION_UP) {
            if (action != MotionEvent.ACTION_DOWN && this.mIsBeingDragged) {
                return true;
            }

            float diff;
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if (!this.k()) {
                        return this.mIsBeingDragged;
                    }

                    this.mInitialMotionY = motionEvent.getY();
                    this.mLastMotionY = this.mInitialMotionY;
                    this.mInitialMotionX = motionEvent.getX();
                    this.mLastMotionX = this.mInitialMotionX;
                    this.mIsBeingDragged = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!this.mScrollingWhileRefreshingEnabled && (this.c())) {
                        return true;
                    }

                    if (!this.k()) {
                        return this.mIsBeingDragged;
                    }

                    float y = motionEvent.getY();
                    float x = motionEvent.getX();
                    float oppositeDiff;
                    switch (getPullToRefreshScrollDirection()) {
                        case HORIZONTAL: {
                            diff = x - this.mLastMotionX;
                            oppositeDiff = y - this.mLastMotionY;
                            break;
                        }
                        default: {
                            diff = y - this.mLastMotionY;
                            oppositeDiff = x - this.mLastMotionX;
                            break;
                        }
                    }

                    float absDiff = Math.abs(diff);
                    if (absDiff <= (float) this.mTouchSlop) {
                        return this.mIsBeingDragged;
                    }
                    if ((this.mFilterTouchEvents) && absDiff <= Math.abs(oppositeDiff)) {
                        return this.mIsBeingDragged;
                    }

                    if (this.mMode.showHeaderLoadingLayout()) {
                        if (diff < 1f) {
                            if (!this.h()) {

                            } else if (!this.s) {

                            } else if (this.getScrollY() >= 0) {

                            } else if (absDiff >= 1f) {
                                if (this.l()) {
                                    this.mLastMotionY = y;
                                    this.mLastMotionX = x;
                                    this.mIsBeingDragged = true;
                                    if (this.mMode != Mode.BOTH) {
                                        return this.mIsBeingDragged;
                                    }

                                    this.mCurrentMode = Mode.PULL_FROM_START;
                                    return this.mIsBeingDragged;
                                }
                            }
                        } else {
                            if (this.l()) {
                                this.mLastMotionY = y;
                                this.mLastMotionX = x;
                                this.mIsBeingDragged = true;
                                if (this.mMode != Mode.BOTH) {
                                    return this.mIsBeingDragged;
                                }
                                this.mCurrentMode = Mode.PULL_FROM_START;
                                return this.mIsBeingDragged;
                            }
                        }
                    }
                    if (!this.mMode.showFooterLoadingLayout()) {
                        return this.mIsBeingDragged;
                    }

                    if (diff > -1f) {
                        return this.mIsBeingDragged;
                    }

                    if (!this.isReadyForPullEnd()) {
                        return mIsBeingDragged;
                    }

                    mLastMotionY = y;
                    mLastMotionX = x;
                    mIsBeingDragged = true;
                    if (mMode != Mode.BOTH) {
                        return mIsBeingDragged;
                    }
                    mCurrentMode = Mode.PULL_FROM_END;
                    break;
            }
            return mIsBeingDragged;
        }

        mIsBeingDragged = false;
        return false;
    }

    protected final void onRestoreInstanceState(Parcelable parcelable) {
        if ((parcelable instanceof Bundle)) {
            Bundle bundle = (Bundle) parcelable;
            setMode(Mode.mapIntToValue(bundle.getInt("ptr_mode", 0)));
            mCurrentMode = Mode.mapIntToValue(bundle.getInt("ptr_current_mode", 0));
            mScrollingWhileRefreshingEnabled = bundle.getBoolean("ptr_disable_scrolling", false);
            mShowViewWhileRefreshing = bundle.getBoolean("ptr_show_refreshing_view", true);
            super.onRestoreInstanceState(bundle.getParcelable("ptr_super"));
            State ptrState = State.mapIntToValue(bundle.getInt("ptr_state", 0));
            if (ptrState == State.REFRESHING || ptrState == State.MANUAL_REFRESHING) {
                this.setState(true, ptrState, new boolean[]{true});
            }
            this.onPtrRestoreInstanceState(bundle);
        } else {
            super.onRestoreInstanceState(parcelable);
        }
    }

    protected final Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        this.onPtrSaveInstanceState(bundle);
        bundle.putInt("ptr_state", this.mState.getIntValue());
        bundle.putInt("ptr_mode", this.mMode.getIntValue());
        bundle.putInt("ptr_current_mode", this.mCurrentMode.getIntValue());
        bundle.putBoolean("ptr_disable_scrolling", this.mScrollingWhileRefreshingEnabled);
        bundle.putBoolean("ptr_show_refreshing_view", this.mShowViewWhileRefreshing);
        bundle.putParcelable("ptr_super", super.onSaveInstanceState());
        return ((Parcelable) bundle);
    }

    @Override
    protected final void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.i();
        ViewGroup.LayoutParams lp = this.mRefreshableView.getLayoutParams();
        switch (this.getPullToRefreshScrollDirection()) {
            case HORIZONTAL:
                if (((LayoutParams) lp).width != w) {
                    ((LayoutParams) lp).width = w;
                    this.mRefreshableView.requestLayout();
                }
                break;
            case VERTICAL:
                if (((LayoutParams) lp).height != h) {
                    ((LayoutParams) lp).height = h;
                    this.mRefreshableView.requestLayout();
                }
                break;
        }
        this.post(new Runnable() {
            @Override
            public void run() {
                requestLayout();
            }
        });
    }

    public final boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.mMode.permitsPullToRefresh()) {
            return false;
        }
        if (!this.mScrollingWhileRefreshingEnabled && (this.c())) {
            return true;
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN
                && motionEvent.getEdgeFlags() != 0) {
            return false;
        }
        PullToRefreshBase$g v0_2;
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                this.D = 0f;
                if (this.k()) {
                    float v0_3 = motionEvent.getY();
                    this.mInitialMotionY = v0_3;
                    this.mLastMotionY = v0_3;
                    v0_3 = motionEvent.getX();
                    this.mInitialMotionX = v0_3;
                    this.mLastMotionX = v0_3;
                    return true;
                } else {
                    return false;
                }
            }
            case MotionEvent.ACTION_MOVE: {
                if (!this.mIsBeingDragged) {
                    return false;
                }
                if (!this.C && this.B != null) {
                    v0_2 = this.B;
                    if (v0_2.mArticleRecentFragment.mFeedDataProvider != null && !v0_2.mArticleRecentFragment.mPullRefreshList.c()) {
                        MobClickCombiner.onEvent(v0_2.mArticleRecentFragment.getActivity(), "pull_refresh", "pull_refresh_count");
                        v0_2.mArticleRecentFragment.mFeedDataProvider.t = System.currentTimeMillis();
                    }
                    this.D = motionEvent.getY();
                    this.C = true;
                }
                this.mLastMotionY = motionEvent.getY();
                this.mLastMotionX = motionEvent.getX();
                this.m();
                return true;
            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                this.C = false;
                OnTouchHook onTouchHook = this.mOnTouchHookRef != null ? this.mOnTouchHookRef.get() : null;
                if (onTouchHook != null) {
                    onTouchHook.mArticleRecentFragment.handleRefreshAdTouchEvent();
                }
                if (!this.mIsBeingDragged) {
                    return false;
                }

                this.mIsBeingDragged = false;
                if (this.mState == State.RELEASE_TO_REFRESH
                        && (this.mOnRefreshListener != null || this.mOnRefreshListener2 != null)) {
                    if (this.B != null) {
                        v0_2 = this.B;
                        if (v0_2.mArticleRecentFragment.mFeedDataProvider != null) {
                            v0_2.mArticleRecentFragment.mFeedDataProvider.u = System.currentTimeMillis();
                        }
                    }

                    this.setState(true, State.REFRESHING, new boolean[]{true});
                    return true;
                }

                if (this.c()) {
                    this.smoothScrollTo(0);
                    return true;
                }

                if (this.B != null) {
                    if (this.D > 0f) {
                        this.B.a((motionEvent.getY() - this.D) / 2f);
                    } else {
                        this.B.a(0f);
                    }
                }

                this.setState(true, State.RESET, new boolean[0]);
                return true;
            }
        }
        return false;
    }

    public void setDisableScrollingWhileRefreshing(boolean disableScrollingWhileRefreshing) {
        this.setScrollingWhileRefreshingEnabled(!disableScrollingWhileRefreshing);
    }

    public void setExtraEnabled(boolean arg2) {
        if (this.mHeaderLayout != null) {
            this.mHeaderLayout.setExtraEnabled(arg2);
        }
    }

    @SuppressLint(value = {"InlinedApi"})
    public final void setHeaderScroll(int value) {
        int maximumPullScroll = this.getMaximumPullScroll();
        value = Math.min(maximumPullScroll, Math.max(-maximumPullScroll, value));
        if (this.mLayoutVisibilityChangesEnabled) {
            if (value < 0) {
                this.mHeaderLayout.setVisibility(VISIBLE);
            } else if (value > 0) {
                this.mFooterLayout.setVisibility(VISIBLE);
            } else {
                this.mHeaderLayout.setVisibility(INVISIBLE);
                this.mFooterLayout.setVisibility(INVISIBLE);
            }
        }

        switch (getPullToRefreshScrollDirection()) {
            case HORIZONTAL: {
                this.scrollTo(value, 0);
                break;
            }
            case VERTICAL: {
                this.scrollTo(0, value);
                break;
            }
        }
    }

    public void setLastUpdatedLabel(CharSequence lastUpdatedLabel) {
        this.getLoadingLayoutProxy().setLastUpdatedLabel(lastUpdatedLabel);
    }

    public void setLoadingDrawable(Drawable loadingDrawable) {
        this.getLoadingLayoutProxy().setLoadingDrawable(loadingDrawable);
    }

    public void setLongClickable(boolean longClickable) {
        this.getRefreshableView().setLongClickable(longClickable);
    }

    public final void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.mOnRefreshListener = onRefreshListener;
        this.mOnRefreshListener2 = null;
    }

    public final void setOnRefreshListener(OnRefreshListener2 onRefreshListener2) {
        this.mOnRefreshListener2 = onRefreshListener2;
        this.mOnRefreshListener = null;
    }

    public void setOnTouchHook(OnTouchHook onTouchHook) {
        this.mOnTouchHookRef = new WeakReference(onTouchHook);
    }

    public void setPullLabel(CharSequence pullLabel) {
        this.getLoadingLayoutProxy().setPullLabel(pullLabel);
    }

    public final void setPullToRefreshEnabled(boolean pullToRefreshEnabled) {
        Mode mode = pullToRefreshEnabled ? Mode.getDefault() : Mode.DISABLED;
        this.setMode(mode);
    }

    public final void setPullToRefreshOverScrollEnabled(boolean arg1) {
        this.mOverScrollEnabled = arg1;
    }

    public final void setRefreshing(boolean arg5) {
        if (!this.c()) {
            this.setState(true, State.MANUAL_REFRESHING, new boolean[]{arg5});
        }
    }

    public void setRefreshingLabel(CharSequence arg2) {
        this.getLoadingLayoutProxy().setRefreshingLabel(arg2);
    }

    public void setReleaseLabel(CharSequence arg3) {
        this.createLoadingLayoutProxy(Mode.BOTH.showHeaderLoadingLayout(), Mode.BOTH.showFooterLoadingLayout()).setReleaseLabel(arg3);
    }

    public void setScrollAnimationInterpolator(Interpolator arg1) {
        this.mScrollAnimationInterpolator = arg1;
    }

    public final void setScrollingWhileRefreshingEnabled(boolean arg1) {
        this.mScrollingWhileRefreshingEnabled = arg1;
    }

    public void setStatisticsListener(PullToRefreshBase$g arg1) {
        this.B = arg1;
    }

    public enum AnimationStyle implements IAnimationStyle {
        ROTATE,
        FLIP;

        static AnimationStyle getDefault() {
            return ROTATE;
        }

        static AnimationStyle mapIntToValue(int i) {
            switch (i) {
                case 1:
                    return FLIP;
                default:
                    return ROTATE;
            }
        }

        @Override
        public final LoadingLayout createLoadingLayout(Context context, Mode mode,
                                                       Orientation orientation, TypedArray attrs) {
            switch (this) {
                case FLIP:
                    return new com.handmark.pulltorefresh.library.internal.b(context, mode, orientation, attrs);
                default:
                    return new com.handmark.pulltorefresh.library.internal.f(context, mode, orientation, attrs);
            }
        }
    }

    public enum Mode {
        DISABLED(0),
        PULL_FROM_START(1),
        PULL_FROM_END(2),
        BOTH(3),
        MANUAL_REFRESH_ONLY(4);

        public static Mode PULL_DOWN_TO_REFRESH;
        public static Mode PULL_UP_TO_REFRESH;

        static {
            PULL_DOWN_TO_REFRESH = PULL_FROM_START;
            PULL_UP_TO_REFRESH = PULL_FROM_END;
        }

        private int mIntValue;

        private Mode(int i) {
            this.mIntValue = i;
        }

        static Mode getDefault() {
            return PULL_FROM_START;
        }

        static Mode mapIntToValue(int i) {
            for (Mode mode : values()) {
                if (i == mode.getIntValue()) {
                    return mode;
                }
            }
            return getDefault();
        }

        public final int getIntValue() {
            return this.mIntValue;
        }

        public final boolean permitsPullToRefresh() {
            return (this == DISABLED || this == MANUAL_REFRESH_ONLY) ? false : true;
        }

        public final boolean showFooterLoadingLayout() {
            return this == PULL_FROM_END || this == BOTH || this == MANUAL_REFRESH_ONLY;
        }

        public final boolean showHeaderLoadingLayout() {
            return this == PULL_FROM_START || this == BOTH;
        }
    }

    public enum Orientation {
        VERTICAL,
        HORIZONTAL
    }

    public enum State {
        RESET(0),
        PULL_TO_REFRESH(1),
        RELEASE_TO_REFRESH(2),
        REFRESHING(8),
        MANUAL_REFRESHING(9),
        OVERSCROLLING(16);

        private int mIntValue;

        private State(int i) {
            this.mIntValue = i;
        }

        static State mapIntToValue(int i) {
            for (State state : values()) {
                if (i == state.getIntValue()) {
                    return state;
                }
            }
            return RESET;
        }

        public final int getIntValue() {
            return this.mIntValue;
        }
    }

    public interface IAnimationStyle {
        LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation orientation,
                                          TypedArray attrs);
    }

    public interface OnRefreshListener2 {
        void onPullDownToRefresh(PullToRefreshBase toRefreshBase);
    }

    public static final class OnRefreshListener {
        public OnRefreshListener() {

        }
    }

    final class OnSmoothScrollListener {
        boolean shouldCallRefreshListener;

        OnSmoothScrollListener(boolean shouldCallRefreshListener) {
            this.shouldCallRefreshListener = shouldCallRefreshListener;
        }
    }

    public static final class OnTouchHook {
        ArticleRecentFragment mArticleRecentFragment;

        public OnTouchHook(ArticleRecentFragment fragment) {
            this.mArticleRecentFragment = fragment;
        }
    }

    final class SmoothScrollRunnable implements Runnable {
        private final Interpolator mInterpolator;
        private final int mScrollToY;
        private final int mScrollFromY;
        private final long mDuration;
        boolean mContinueRunning = true;
        private OnSmoothScrollListener mListener;
        private long mStartTime = -1;
        private int mCurrentY = -1;

        public SmoothScrollRunnable(int fromY, int toY, long duration, OnSmoothScrollListener listener) {
            this.mScrollFromY = fromY;
            this.mScrollToY = toY;
            this.mInterpolator = mScrollAnimationInterpolator;
            this.mDuration = duration;
            this.mListener = listener;
        }

        public final void run() {
            if (this.mStartTime == -1) {
                this.mStartTime = System.currentTimeMillis();
            } else {

                mCurrentY = mScrollFromY - Math.round(
                        this.mInterpolator.getInterpolation((float) Math.max(Math.min((System.currentTimeMillis() - this.mStartTime) * 1000 / this.mDuration, 1000), 0) / 1000f) * (float) (this.mScrollFromY - this.mScrollToY));
                setHeaderScroll(this.mCurrentY);
            }

            if (this.mContinueRunning && this.mScrollToY != this.mCurrentY) {
                if (Build.VERSION.SDK_INT >= 16) {
                    postOnAnimation(this);
                } else {
                    postDelayed(this, 16);
                }
            } else if (this.mListener != null) {
                if (mListener.shouldCallRefreshListener) {
                    PullToRefreshBase.this.callRefreshListener();
                }
            }
        }
    }

    public static final class PullToRefreshBase$g {
        private final ArticleRecentFragment mArticleRecentFragment;

        public PullToRefreshBase$g(ArticleRecentFragment fragment) {
            this.mArticleRecentFragment = fragment;
        }

        public final void a(float cancelDistance) {
            int dp = 0;
            if (cancelDistance > 0f) {
                dp = UIUtils.px2dp(this.mArticleRecentFragment.getContext(), cancelDistance);
            }
            MobClickCombiner.onEvent(this.mArticleRecentFragment.getActivity(), "pull_refresh", "pull_refresh_cancel_count", 0, (long) dp);
            Logger.d("cancel_distance", String.valueOf(dp));
        }
    }
}

